<?
namespace xto\data\service;

use xto\data\model\PayModel;
use xto\data\entities\PayInfo;
use xto\data\Factory;

class PayService{
	static function create($payinfo){
		$isadd=true;
		$f=Factory::getPayFactory();
		$data=[
			'paytype' 		=> $payinfo->paytype,
			'config'		=> $payinfo->config,
			'remark'		=> $payinfo->remark, 
			'appid'			=> $f->app->appid,
			'payid'			=> $payinfo->payid
			]; 
		$c=$f->where('appid',$f->app->appid)
			->where('paytype',$payinfo->paytype)
			->count();
		if($c>0 && (empty($payinfo->payid) or $payinfo->payid==0)){
			return false;
		}else{
			if($payinfo->payid>0) $isadd=false;
			if($isadd){
				$f->save($data);
				return true;
			}else{
				$f->update($data);
				return true;
			} 
		}
		return false;
	}

	static function getPay($type){
		$c=Factory::getPayFactory();
		$m= $c::get(['appid' => $c->app->appid,'paytype'=>$type]);
		$info=new PayInfo();
		$info->payid 		= $m['payid'];
		$info->paytype 		= $m['paytype'];
		$info->config 		= $m['config'];
		$info->remark 		= $m['remark'];
		$info->appid 		= $m['appid'];  
		return $info;
	}

	static function selectAll($pagezie){
		$result=array();
		$list = Factory::getPayFactory()->order('payid', 'asc')->paginate($pagezie);
		$page = $list->render();
		$result['list']=$list;
		$result['page']=$page;
		return $result;
	}
}